package com.cloud.controller;


import com.cloud.constent.Constant;
import com.cloud.entity.CommonResult;
import com.cloud.entity.Payment;
import com.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

@Slf4j
@RestController
@RefreshScope
public class PaymentController {
    @Resource
    PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/payment/select/{id}")
    public CommonResult<Payment> select(@PathVariable Long id) {
        Payment payment = null;
        int code = 0;
        String message = "";
        try {
            payment = this.paymentService.selectByPrimaryKey(id);
            if (null != payment) {
                code = Constant.OKCODE;
                message = Constant.OKCODEMESSAGE;
            } else {
                code = Constant.ERRORCODE;
                message = Constant.ERRORCODEMESSAGE;
            }
        } catch (Exception e) {
            code = Constant.ERRORCODE;
            message = Constant.ERRORCODEMESSAGE;
            log.info(e.getMessage());
        }
        return new CommonResult<>(code, message+serverPort, payment);
    }


    @RequestMapping(value = "/payment/insert")
    public CommonResult<Payment> insert(@RequestBody Payment payment) {
        int code = 0;
        String message = "";
        try {
            final int re = this.paymentService.insert(payment);
            if (re > 0) {
                code = Constant.OKCODE;
                message = Constant.OKCODEMESSAGE;
            } else {
                code = Constant.ERRORCODE;
                message = Constant.ERRORCODEMESSAGE;
            }
        } catch (Exception e) {
            code = Constant.ERRORCODE;
            message = Constant.ERRORCODEMESSAGE;
            log.info(e.getMessage());
        }

        return new CommonResult<>(code, message, null);
    }


    public static void main(String[] args) {
        CommonResult commonResult = new CommonResult(1, "1");
        CommonResult commonResult2 = new CommonResult(2, "2");
        List<CommonResult> list = new ArrayList();
        list.add(commonResult);
        list.add(commonResult2);
        list.removeIf(new Predicate<CommonResult>() {
            @Override
            public boolean test(CommonResult commonResult) {
                if (commonResult.getCode() == 1) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        list.forEach(commonResult1 -> {
            System.out.println(commonResult1.getMessage());
        });
    }



    @GetMapping(value = "/payment/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            log.info("****element: " +element);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance:instances) {
            log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }
}
